﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DiagramDesigner;

namespace BDSim
{
    /// <summary>
    /// AircraftDynamicSetWnd.xaml 的交互逻辑
    /// </summary>
    public partial class AircraftDynamicSetWnd : Window
    {
        public AircraftDynamicSetWnd()
        {
            InitializeComponent();
            AirViewModel.DlgGetTrackIDorder = GetTrackIdOrder;
            AirViewModel.DlgSaveXMLFile = SaveScenXMLFile;
            AirViewModel.DlgLoadXMLFile = LoadScenXMLFile;
            AirViewModel.DlgDisplayTrackWnd = ShowTrackWnd;
            //AirViewModel.DlgDisplayInertiaWnd = ShowInertiaParamSetWnd;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            TrackConfigView.MyDesigner.DlgAddTrackModel = AirViewModel.AddTrackModel;
            TrackConfigView.MyDesigner.DlgDisplayProperty = DisplayTrackProperty;
            TrackConfigView.MyDesigner.DlgDeleteTrackModel = AirViewModel.DeleteTrackModel;

            switch (AirViewModel.Parent.Type)
            {
                case EUserType.StaticUser:
                    break;
                case EUserType.Car:

                    trackTypeView1.CarExpander.Visibility = Visibility.Visible;
                    trackTypeView1.AirExpander.Visibility = Visibility.Collapsed;
                    trackTypeView1.BoatExpander.Visibility = Visibility.Collapsed;
                    break;
                case EUserType.Aircraft:
                    trackTypeView1.CarExpander.Visibility = Visibility.Collapsed;
                    trackTypeView1.AirExpander.Visibility = Visibility.Visible;
                    trackTypeView1.BoatExpander.Visibility = Visibility.Collapsed;
                    break;
                case EUserType.Boat:
                    trackTypeView1.CarExpander.Visibility = Visibility.Collapsed;
                    trackTypeView1.AirExpander.Visibility = Visibility.Collapsed;
                    trackTypeView1.BoatExpander.Visibility = Visibility.Visible;
                    break;
                case EUserType.Missile:
                    break;
                case EUserType.SpaceVehicle:
                    break;
                case EUserType.CustomUser:
                    break;
                default:
                    break;
            }
            
            //TrackConfigView.MyDesigner.DlgWavViewBntEnable = this.TracktoolBarView.WavBtnEnabled;
        }

        #region 方法实现
        
        //找出所有连线好的轨迹模型，并按顺序把GUID取出来作为返回值
        List<string> GetTrackIdOrder()
        {
            //找出所有的连线
            List<Connection> connectionItems = TrackConfigView.MyDesigner.Children.OfType<Connection>().ToList();
            List<DesignerItem> designerItems = TrackConfigView.MyDesigner.Children.OfType<DesignerItem>().ToList();
            //准备一个容器，用来存储按连线顺序排序的轨迹模型编号
            List<string> TrackIdOrder = new List<string>();
            //首先存下“起始设定”的编号UID
            foreach (var item in designerItems)
            {
                if (item.Uid == "Start")
                {
                    TrackIdOrder.Add(item.ID.ToString());
                    break;
                }
            }
            //计算连线的所有轨迹模型个数
            int designerItemsCount = connectionItems.Count() + 1;
            for (int i = 1; i < designerItemsCount; i++)
            {
                foreach (var item in connectionItems)
                {
                    if (item.Source.ParentDesignerItem.ID.ToString() == TrackIdOrder[i - 1])
                    {
                        TrackIdOrder.Add(item.Sink.ParentDesignerItem.ID.ToString());
                        //如果当前节点已经是“停止运动”轨迹，则直接返回
                        //if (item.Sink.ParentDesignerItem.Uid == "Stop")
                        //{
                        //    return TrackIdOrder;
                        //}
                        //把处理过的连线删掉，避免下次轮询不必要的访问
                        connectionItems.Remove(item);
                        break;
                    }
                }
            }
            if (designerItemsCount == 1)
            {
                TrackIdOrder.Add(((DesignerItem)TrackConfigView.MyDesigner.Children[0]).ID.ToString());
            }
            return TrackIdOrder;
        }

        //保存场景XML
        public void SaveScenXMLFile(string path)
        {
            TrackConfigView.MyDesigner.SaveDiagramXML(path);
        }

        //加载场景
        public void LoadScenXMLFile(string path)
        {
            TrackConfigView.MyDesigner.LoadXMLDataFile(path);
        }

        /// <summary>
        /// 显示轨迹编辑窗口
        /// </summary>
        public void ShowTrackWnd()
        {
            //this.MainWindowSimSys.ResizeMode = System.Windows.ResizeMode.CanResize;
            this.ADockTrackEdit.Visibility = System.Windows.Visibility.Visible;
            this.TracktoolBarView.Visibility = System.Windows.Visibility.Visible;

        }

        /// <summary>
        /// 显示属性
        /// </summary>
        /// <param name="uid"></param>
        public void DisplayTrackProperty(Guid uid)
        {
            foreach (var item in AirViewModel.TrackModelList)
            {
                if (item.UID == uid.ToString())
                {
                    ppgNodeProperties.SelectedObject = item;
                    break;
                }
            }
        }

        #endregion 方法实现
    }
}
